//
//  ContactMessage.m
//  uteped_sdk
//
//  Created by mateng on 2024/2/20.
//

#import "ContactMessage.h"

#define M_syncContactsInfoToDevice @"syncContactsInfoToDevice"
#define M_syncSosCallInfo @"syncSosCallInfo"
#define M_syncSmsReplyContent @"syncSmsReplyContent"


@implementation ContactMessage

- (NSString *)getPath {
    return @"contact";
}

- (BOOL) methodCallConsumed:(NSString*)method params:(NSDictionary*)params result:(FlutterResult)result
{
    __weak typeof(self)weakSelf = self;
    BOOL consumed = YES;
    if ([super methodCallConsumed:method params:params result:result])
        return YES;
    
    assert(method != nil);
    NSNumber *enable = nil;
    NSNumber *number = nil;
    if ([method isEqualToString:M_syncContactsInfoToDevice]) {
        
        if (![self isConnected]) {
            result(@NO);
            return YES;
        }
        
        NSArray *contactsArray = [params objectForKey:TYPE_LIST];
        NSMutableArray<UTEModelContactInfo *> *contactsList = [NSMutableArray array];
        for (NSDictionary *item in contactsArray) {
            UTEModelContactInfo* contactInfo = [[UTEModelContactInfo alloc] init];
            contactInfo.name = [item objectForKey:KEY_NAME];
            contactInfo.number = [item objectForKey:KEY_PHONE];
            
            [contactsList addObject:contactInfo];
        }
        
        [[UTEDeviceMgr sharedInstance] setAddressBookModel:contactsList Block:^(NSInteger sendTime, NSInteger errorCode) {
            if (errorCode == UTEDeviceErrorNil) {
                result(@YES);
            }
            else {
                result(@NO);
            }
            
            [weakSelf.eventHander publishEvent: [NSDictionary dictionaryWithObjectsAndKeys:
                                              @"contact", KEY_PATH,
                                              @"syncContactsInfoToDevice", KEY_METHOD,
                                              ACTION_SYNC_CONTACT_STATE, KEY_ACTION,
                                              @(2), KEY_VALUE, nil] async:YES];
        }];
    }
    else if ([method isEqualToString:M_syncSosCallInfo]) {
        if (![self isConnected]) {
            result(@NO);
            return YES;
        }
        
        NSArray* sosArr = [params objectForKey:TYPE_LIST];
        NSMutableArray *sosNumbers = [NSMutableArray array];
        if (sosArr != nil && sosArr.count > 0) {
            for (NSDictionary *item in sosArr) {
                UTEModelContactInfo *ci = [[UTEModelContactInfo alloc] init];
                ci.name = [item objectForKey:KEY_NAME];
                ci.number = [item objectForKey:KEY_PHONE];
                [sosNumbers addObject: ci];
            }
        }
        
        [[UTEDeviceMgr sharedInstance] setSOSNumberModel:(NSArray<UTEModelContactInfo *> *)sosNumbers Block:^(NSInteger errorCode) {
            result(errorCode == UTEDeviceErrorNil ? @YES : @NO);
        }];
    }
    else if ([method isEqualToString:M_setSmsReplyEnable]) {
        enable = [params objectForKey:KEY_ENABLE];
        if (enable != nil) {
            result(@YES);
        }
        else {
            result(@NO);
        }
    }
    else if ([method isEqualToString:M_setSmsRemindEnable]) {
        if (![self isConnected]) {
            result(@NO);
            return YES;
        }
        
        NSNumber *number = [params objectForKey:KEY_ENABLE];
        UTEModelANCSInfo *model = [UTEModelANCSInfo new];
        model.appId = [UTEDeviceMgr getBundleIDFromApp: UTEAppSMS];
        model.enable = [number boolValue] ? 1 : 0;
        
        NSArray *appArray = [NSArray arrayWithObjects:model, nil];
        [[UTEDeviceMgr sharedInstance] setNotifyAppListModel:appArray block:^(NSInteger errorCode, NSDictionary *uteDict) {
            [weakSelf log:[NSString stringWithFormat:@"setSmsRemindEnable %ld", errorCode]];
            result(errorCode == UTEDeviceErrorNil ? @YES : @NO);
        }];
    }
    else if ([method isEqualToString:M_setCallRemindEnable]) {
        if (![self isConnected]) {
            result(@NO);
            return YES;
        }
        
        enable = [params objectForKey:KEY_ENABLE];
        if (enable != nil) {
            [[UTEDeviceMgr sharedInstance] setNotificationConfig:YES incomingEnable:[enable boolValue] block:^(NSInteger errorCode,NSDictionary *uteDict) {
                [weakSelf log:[NSString stringWithFormat:@"setCallRemindEnable %ld", errorCode]];
                result(errorCode == UTEDeviceErrorNil ? @YES : @NO);
            }];
        }
        else {
            result(@NO);
        }
    }
    else if ([method isEqualToString:M_setCallRemindHangup]) {
        enable = [params objectForKey:KEY_ENABLE];
        if (enable != nil) {
            [[SPHelper shareInstance] putObject:M_setCallRemindHangup value:enable];
            result(@YES);
        }
        else {
            result(@NO);
        }
        result(@YES);
    }
    else if ([method isEqualToString:M_syncSmsReplyContent]) {
        result([NSDictionary dictionaryWithObjectsAndKeys:@YES, KEY_SUCCESS, @(10000), KEY_ERROR_CODE, nil]);
    }
    else {
        consumed = NO;
    }
    
    return consumed;
}

@end
